function test1() {
  const target = { a: 1 };
  const source1 = { b: 2 };
  const source2 = { c: 3 };
  Object.assign(target, source1, source2);
  console.log(target); // { a: 1, b: 2, c: 3 }
}
function test2() {
  const target = { a: 1, b: 2 };
  const source = { b: 3, c: 4 };
  Object.assign(target, source);
  console.log(target); // { a: 1, b: 3, c: 4 }
}
// 只复制可枚举属性
// 浅拷贝：Object.assign() 执行的是浅拷贝，而不是深拷贝。这意味着如果源对象的属性值是对象
const friendsData = [
  {
    id: 1,
    avatar: "/static/person/1.jpg",
    username: "是阿玲啊",
    desc: "这是用户1的描述信息",
    followed: true,
  },
  {
    id: 2,
    avatar: "/static/person/2.jpg",
    username: "大汉虾",
    desc: "这是用户2的描述信息",
    followed: false,
  },
];
function test3() {
  let target2 = [];
  Object.assign(target2, friendsData);
  console.log(target2); // [{ id: 1, avatar: "/static/person/1.jpg", username: "是阿玲啊", desc: "这是用户1的描述信息", followed: true }, { id: 2, avatar: "/static/person/2.jpg", username: "大汉虾", desc: "这是用户2的描述信息", followed: false }]
  friendsData[0].desc = "xxxxxx";
  console.log(target2); // 浅拷贝, 所以修改 friendsData[0].desc 也会影响 target2[0].desc
}

function test4() {
  const deepCopy = (obj) => JSON.parse(JSON.stringify(obj));
  const friendsData2 = deepCopy(friendsData);
  console.log(friendsData2); // [{ id: 1, avatar: "/static/person/1.jpg", username: "是阿玲啊", desc: "这是用户1的描述信息", followed: true }, { id: 2, avatar: "/static/person/2.jpg", username: "大汉虾", desc: "这是用户2的描述信息", followed: false }]
  friendsData[0].desc = "xxxxxx";
  console.log("-------------");
  console.log(friendsData2); // [{ id: 1, avatar: "/static/person/1.jpg", username: "是阿玲啊", desc: "xxxxxx", followed: true }, { id: 2, avatar: "/static/person/2.jpg", username: "大汉虾", desc: "这是用户2的描述信息", followed: false }]
}

// test4(); // 可以看到深拷贝之后，修改源对象不会影响目标对象

// node.js默认不支持import语法，支持commonjs规范
// 1. 完整导入 Lodash（简单但体积较大）
//   import _ from "lodash";

// 2. 按需导入（推荐，减少打包体积）
// import cloneDeep from "lodash/cloneDeep";

// 3. CommonJS 规范（如 Node.js 或旧项目）
//   const _ = require("lodash");
const cloneDeep = require("lodash/cloneDeep");
function test5() {
  // 示例：深拷贝嵌套数据
  const originalData = {
    name: "张三",
    age: 28,
    hobbies: ["篮球", "游戏"], // 嵌套数组
    address: {
      // 嵌套对象
      province: "广东",
      city: "深圳",
      detail: {
        street: "科技园路",
      },
    },
    sayHi: () => console.log("Hi"), // 函数
    reg: /^1[3-9]\d{9}$/, // 正则对象
  };

  // 执行深拷贝
  const clonedData = cloneDeep(originalData); // 或 _.cloneDeep(originalData)
  console.log(clonedData);
  originalData.address.city = "北京"; // 修改源对象
  console.log(clonedData);
}
test5();
